package models

import (
	"comment/util"
	"github.com/astaxie/beego/orm"
	"strconv"
)

type Report struct {
	Id                 int
	Report_num         string
	User_id            int    `valid:"Required"`
	User_name          string `valid:"Required"`
	Phone              string `valid:"Required"`
	Be_user_id         int    `valid:"Required"`
	Be_user_name       string `valid:"Required"`
	Be_report_phone    string `valid:"Required"`
	Be_report_article  int    `valid:"Required"`
	Be_report_address  string `valid:"Required"`
	Article_title      string `valid:"Required"`
	Report_description string `valid:"Required"`
	Report_content     string `valid:"Required"`
	Comment_content    string `valid:"Required"`
	Report_time        string
	Audit_status       int
	Audit_opinion      string
	Audit_time         string
	Audit_user_id      int
	Audit_user_name    string
	Refused_status     int
	Is_malicious       int
}

func init() {
	orm.RegisterModelWithPrefix("comment_", new(Report))
}
func SaveReport(report *Report) (int64, error) {
	o := orm.NewOrm()
	id, err := o.Insert(report)
	return id, err
}

func PageReport(query map[string]string, status int, pageNo int, size int) util.Page {
	o := orm.NewOrm()
	var report Report
	var list []Report
	qs := o.QueryTable(report)
	for k, v := range query {
		qs = qs.Filter(k, v)
	}
	count, _ := qs.Filter("Audit_status", status).Limit(-1).Count()
	qs = qs.OrderBy("-Report_time")
	qs.RelatedSel().Filter("Audit_status", status).Limit(size).Offset((pageNo - 1) * size).All(&list)
	c, _ := strconv.Atoi(strconv.FormatInt(count, 10))
	return util.PageUtil(c, pageNo, size, list)
}

func FindReportByNum(num string) Report {
	o := orm.NewOrm()
	var report Report
	o.QueryTable(report).Filter("Report_num", num).One(&report)
	return report
}

func UpdateReport(report *Report) (int64, error) {
	o := orm.NewOrm()
	return o.Update(report, "Audit_status", "Audit_opinion", "Audit_time", "Audit_user_id", "Audit_user_name", "Refused_status", "Is_malicious")
}
func FindReportById(id int) Report {
	o := orm.NewOrm()
	var report Report
	o.QueryTable(report).Filter("Id", id).One(&report)
	return report
}
func FindCountByUserIdAndStatus(userId int, status int) (int64, error) {
	o := orm.NewOrm()
	var report Report
	qs := o.QueryTable(report)
	if status == 0 || status == 1 || status == 2 {
		qs = qs.Filter("Audit_status", status)
	}
	count, err := qs.Filter("User_id", userId).Count()
	return count, err
}
func FindRportByReportUserId(userId int) ([]Report, error) {
	o := orm.NewOrm()
	var report Report
	var list []Report
	qs := o.QueryTable(report)
	_, err := qs.Filter("User_id", userId).All(&list)
	return list, err
}

func FindBeCountByUserIdAndStatus(beUserId int, status int) (int64, error) {
	o := orm.NewOrm()
	var report Report
	qs := o.QueryTable(report)
	if status == 0 || status == 1 || status == 2 {
		qs = qs.Filter("Audit_status", status)
	}
	count, err := qs.Filter("Be_user_id", beUserId).Count()
	return count, err
}
